// app.js
const express = require('express')
const createError = require('http-errors')
const session = require('express-session')
const passport = require('passport')
const { verifyUser } = require('./authenticate')
const thirdPartRouter = require('./thirdPartRouter')


const router = require('./router')


const app = express()

// 配置使用session
// app.use(session({
//     secret: '12345-67890-09876-54321',
//     resave: false,
//     saveUninitialized: false,
//     cookie: { secure: false, maxAge: 800000, httpOnly: false },
//     name: 'session-id'
// }))

// 解析post请求体
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

//初始化passport
app.use(passport.initialize())
// app.use(passport.session())


app.get('/', (req, res) => {
    res.end('welcome to my homepage')
})

// 注册登录处理
app.use(router)
app.use(thirdPartRouter)

// 限制路由
app.get('/document', verifyUser, (req, res, next) => {
    res.end('登录后可见的内容')
})



// 错误处理
app.use((req, res, next) => {
    next(createError(404))
})
app.use((err, req, res, next) => {
    res.status(err.status || 500)
    res.end(err.toString())
})

app.listen(3000)